package erfgame.core.world.terrain;

import java.util.WeakHashMap;

import erfgame.core.world.World;

public class CachingTerrainColorSourceIndexFactoryProxy implements
		TerrainColorSourceIndexFactory {

	private WeakHashMap<World, TerrainColorSourceIndex> cached;
	private TerrainColorSourceIndexFactory proxied;
	
	public CachingTerrainColorSourceIndexFactoryProxy( TerrainColorSourceIndexFactory proxied ) {
		this.cached = new WeakHashMap<World, TerrainColorSourceIndex>(1);
		this.proxied = proxied;
	}
	
	public TerrainColorSourceIndex createIndex(World world) {
		TerrainColorSourceIndex result = cached.get(world);
		if( result == null ) {
			result = proxied.createIndex(world);
			cached.put(world, result);
		}
		return result;
	}

}
